package cetb.data;
import java.sql.*;
public class ConnectionManager {
    private static ConnectionManager cm;
    /**
	 * @uml.property  name="dBDriver"
	 * @uml.associationEnd  
	 */
    private Driver dBDriver = null;
    private static final String dBUri = "jdbc:mysql://127.0.0.1:3306/cetb";
    private static final String driverName = "com.mysql.jdbc.Driver";
    private static final String password = "practica";
    private static final String username = "practica";
    private ConnectionManager() {
        try {
            dBDriver = (Driver) Class.forName(driverName).newInstance();
            DriverManager.registerDriver(dBDriver);
        } catch (Exception e) {
            System.err.println("Unable to register JDBC Driver");
            e.printStackTrace();
        }
    }
    public static synchronized ConnectionManager getInstance() {
        if (cm == null) {cm = new ConnectionManager();}
        return cm;
    }
    public Connection checkOut() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(dBUri, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public void checkIn(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    protected void finalize() {
        System.out.println("Llamando a finalize de ConnectionManager");
        try {
            DriverManager.deregisterDriver(dBDriver);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
  }
